import requests
import os

# 获取Jenkins构建信息
job_name = os.environ.get('JOB_NAME', 'Unknown Job')
build_number = os.environ.get('BUILD_NUMBER', 'Unknown Build Number')
build_user = os.environ.get('BUILD_STATUS', 'Unknown User')
build_time = os.environ.get('BUILD_TIME', 'Unknown Time')
branch_name = os.environ.get('BRANCH_NAME', 'Unknown Branch')

# 构建消息内容
message = f"Jenkins构建完成:\n构建状态: {build_user}\n构建时间: {build_time}\n构建分支: {branch_name}\n" \
          f"任务名称: {job_name}\n构建次数: {build_number}"

# 发送消息
send_url = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=90fc4255-c204-4708-acda-41d0335d3292'
data = {
    "msgtype": "text",
    "text": {
        "content": message
    }
}

response = requests.post(send_url, json=data)
if response.json()['errcode'] == 0:
    print('消息发送成功')
else:
    print(f'消息发送失败: {response.json()["errmsg"]}')
